package com.meiqiongproperty.yiguanjia.fragment.home.adapter.tenement;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.meiqiongproperty.yiguanjia.BR;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.CommunityInformationItemBinding;
import com.meiqiongproperty.yiguanjia.databinding.WuyeGgItemBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.Information;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.Notice;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.adapter.tenement
 * @User ChenJing
 * @Date 2017/5/3
 */

public class wwggAdapter extends RecyclerView.Adapter<wwggAdapter.BindingHolder> {
    Context context;
    WuyeGgItemBinding binding;
    List<Notice.NoticeBean> informationList=new ArrayList<>();
    LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;


    public wwggAdapter(Context context, List<Notice.NoticeBean> informationList) {
        this.context = context;
        this.informationList=informationList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public wwggAdapter.BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding= DataBindingUtil.inflate(inflater, R.layout.wuye_gg_item,parent,false);
        wwggAdapter.BindingHolder holder = new wwggAdapter.BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(final wwggAdapter.BindingHolder holder, int position) {
        Notice.NoticeBean information=informationList.get(position);
        //binding.icon.setBackgroundResource(information.getImageurl());
      //  Picasso.with(context).load(product.getImg_resourceId()).centerInside().into(holder.binding.);
        Glide.with(context).load(information.getImage()).into(holder.binding.wygg);

        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return informationList.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder {
        WuyeGgItemBinding binding;

        public WuyeGgItemBinding getBinding() {
            return binding;
        }

        public void setBinding(WuyeGgItemBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
